import { createRouter, createWebHistory } from "vue-router";

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: "/",
      component: () => import("@/components/Layout/index.vue"),
      meta: {needLogin: true},
      children: [
        {
          path: '',
          name: "首页",
          component: () => import("@/views/Home/Home.vue"),
          meta: { keepAlive: true, isAuth: true, needLogin: true },
        },
        // 智能寻人
        {
          path: "intelligent-search",
          name: "智能寻人",
          meta: { keepAlive: true, isAuth: true, needLogin: true },
          redirect: { name: "人脸识别比对" },
          children: [
            {
              path: "facial-recognition",
              name: "人脸识别比对",
              component: () => import("@/views/IntelligentSearch/FacialRecognition.vue"),
              meta: { keepAlive: true, isAuth: true, needLogin: true },
            },
            {
              path: "face-management",
              name: "人脸样本管理",
              component: () => import("@/views/IntelligentSearch/FaceManagement.vue"),
              meta: { keepAlive: true, isAuth: true, needLogin: true },
            },
            {
              path: "multimodal-search",
              name: "多模态搜索",
              component: () => import("@/views/IntelligentSearch/MultimodalSearch.vue"),
              meta: { keepAlive: true, isAuth: true, needLogin: true },
            },
          ],
        },

        // 寻人大厅
        {
          path: "search-hall",
          name: "寻人大厅",
          meta: { keepAlive: true, isAuth: true, needLogin: true },
          redirect: { name: "失踪者档案库" },
          children: [
            {
              path: "missing-archives",
              name: "失踪者档案库",
              component: () => import("@/views/SearchHall/MissingArchives.vue"),
              meta: { keepAlive: true, isAuth: true, needLogin: true },
            },
            {
              path: "clue-center",
              name: "线索协作中心",
              component: () => import("@/views/SearchHall/ClueCenter.vue"),
              meta: { keepAlive: true, isAuth: true, needLogin: true },
            },
            {
              path: "stories",
              name: "找回者故事",
              component: () => import("@/views/SearchHall/Stories.vue"),
              meta: { keepAlive: true, isAuth: true, needLogin: true },
            },
          ],
        },

        // 发布寻人
        {
          path: "publish-search",
          name: "发布寻人",
          meta: { keepAlive: true, isAuth: true, needLogin: true },
          redirect: { name: "启事创建" },
          children: [
            {
              path: "create-notice",
              name: "启事创建",
              component: () => import("@/views/PublishSearch/CreateNotice.vue"),
              meta: { keepAlive: true, isAuth: true, needLogin: true },
            },
            {
              path: "manage-notice",
              name: "启事管理",
              component: () => import("@/views/PublishSearch/ManageNotice.vue"),
              meta: { keepAlive: true, isAuth: true, needLogin: true },
            },
            {
              path: "emergency-response",
              name: "紧急响应",
              component: () => import("@/views/PublishSearch/EmergencyResponse.vue"),
              meta: { keepAlive: true, isAuth: true, needLogin: true },
            },
          ],
        },

        // 资讯中心
        {
          path: "info-center",
          name: "资讯中心",
          meta: { keepAlive: true, isAuth: true, needLogin: true },
          redirect: { name: "政策法规" },
          children: [
            {
              path: "policies",
              name: "政策法规",
              component: () => import("@/views/InfoCenter/Policies.vue"),
              meta: { keepAlive: true, isAuth: true, needLogin: true },
            },
            {
              path: "safety-education",
              name: "安全教育",
              component: () => import("@/views/InfoCenter/SafetyEducation.vue"),
              meta: { keepAlive: true, isAuth: true, needLogin: true },
            },
          ],
        },

        // 系统监测
        {
          path: "system-monitor",
          name: "系统监测",
          redirect: { name: "运行看板" },
          meta: { keepAlive: true, isAuth: true, needLogin: true },
          children: [
            {
              path: "dashboard",
              name: "运行看板",
              component: () => import("@/views/SystemMonitor/Dashboard.vue"),
              meta: { keepAlive: true, isAuth: true, needLogin: true },
            },
            {
              path: "data-security",
              name: "数据安全",
              component: () => import("@/views/SystemMonitor/DataSecurity.vue"),
              meta: { keepAlive: true, isAuth: true, needLogin: true },
            },
            {
              path: "collaboration",
              name: "协作管理",
              component: () => import("@/views/SystemMonitor/Collaboration.vue"),
              meta: { keepAlive: true, isAuth: true, needLogin: true },
            },
          ],
        },

        // 用户中心
        {
          path: "user-center",
          name: "用户中心",
          meta: { keepAlive: true, isAuth: true, needLogin: true },
          redirect: { name: "个人账户" },
          children: [
            {
              path: "account",
              name: "个人账户",
              component: () => import("@/views/UserCenter/Account.vue"),
              meta: { keepAlive: true, isAuth: true, needLogin: true },
            },
            {
              path: "notifications",
              name: "消息通知",
              component: () => import("@/views/UserCenter/Notifications.vue"),
              meta: { keepAlive: true, isAuth: true, needLogin: true },
            },
            {
              path: "privacy",
              name: "隐私控制",
              component: () => import("@/views/UserCenter/Privacy.vue"),
              meta: { keepAlive: true, isAuth: true, needLogin: true },
            },
          ],
        },
      ],
    },
    {
      path: '/Login',
      name: "登录页",
      component: () => import("@/components/Login/LoginPage.vue"),
    },
  ],
});
//
// // 路由守卫
// router.beforeEach((to, from, next) => {
//   if (to.meta.needLogin) {
//     // 判断是否已登录
//     if (localStorage.getItem("token")) {
//       next();  // 已登录
//     } else {
//       // 没登录，重定向到登录页
//       next({ name: "登录页", query: { redirect: to.fullPath } });
//     }
//   } else {
//     next();  // 不需要登录
//   }
// });

export default router;
